package csel.controller.commands;

import csel.controller.GameController;
import csel.model.GameFactory;
import csel.model.Model;
import csel.model.GameCommand;
import csel.model.Occupation;
import csel.view.AreaViewport;
import csel.view.NewGameDialog;
import csel.view.Splasher;
import csel.view.StatusViewport;

public class NewCommand implements GameCommand
{
	private boolean go;
	
	private Model model;
	private StatusViewport statusViewport;
	private AreaViewport areaViewport;
	private Splasher splashViewport;
	private String fileName;
	private GameCommand gameCommand;
	private Occupation occupation;
	private String name;
	
	//constructs new New command
	public NewCommand(Model model, StatusViewport statusViewport, Splasher splashViewport, AreaViewport areaViewport)
	{
		go = false;
		this.model = model;
		this.statusViewport = statusViewport;
		NewGameDialog newGameDialog = new NewGameDialog();
		go = newGameDialog.show();
		this.occupation = newGameDialog.getOccupation();
		this.name = newGameDialog.getName();
		if(GameController.infoMode)System.out.println("[GAMECOMMAND] "+ name);
		if(GameController.infoMode)System.out.println("[GAMECOMMAND] "+ occupation.getClass());
		this.splashViewport = splashViewport;
		this.gameCommand = new ChangeToGameViewCommand(splashViewport, statusViewport, areaViewport);
	}
	
	//
	public void execute()
	{
		if (go)
		{
			try
			{
				GameFactory.newGame(occupation, name, model);
			}
			catch (java.io.IOException ex)
			{
				javax.swing.JOptionPane.showMessageDialog(null, "Could not load map.");
			}
			
			statusViewport.modelUpdate(model);
			splashViewport.accept(gameCommand);
		}
		if(Model.infoMode)System.out.println("[MODEL] the New command has been called");
	}
	
	public String toString()
	{
		return this.getClass().getSimpleName();
	}

}